package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentCommParameters;
import com.mxpio.erp.common.equipment.entity.EquipmentCommParametersDetail;
import com.mxpio.erp.common.equipment.service.EquipmentCommParametersService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;


@Tag(name = "EquipmentCommParametersController}", description = "公共参数")
@RestController
@RequestMapping("/erp/equipment/parameters/")
public class EquipmentCommParametersController {

    @Autowired
    private EquipmentCommParametersService equipmentCommParametersService;


    @GetMapping("list")
    @Operation(summary = "公共参数列表", description = "获取公共参数列表（无分页）", method = "GET")
    public Result<List<EquipmentCommParameters>> list(@Parameter(description = "查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentCommParameters> list = equipmentCommParametersService.list(EquipmentCommParameters.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "公共参数列表", description = "获取公共参数列表（分页）", method = "GET")
    public Result<Page<EquipmentCommParameters>> page(@Parameter(description = "查询参数") Criteria criteria, @Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo - 1, pageSize);
        Page<EquipmentCommParameters> pageResult = equipmentCommParametersService.listPage(EquipmentCommParameters.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取公共参数列表", description = "根据ID获取公共参数列表", method = "GET")
    public Result<EquipmentCommParameters> getById(@PathVariable(name = "id", required = true) String id) {
        EquipmentCommParameters equipmentCommParameters = equipmentCommParametersService.getById(EquipmentCommParameters.class, id);
        return Result.OK(equipmentCommParameters);
    }

    @RequestMapping("add")
    @Operation(summary = "保存公共参数", description = "保存公共参数")
    public Result<EquipmentCommParameters> add(@RequestBody EquipmentCommParameters equipmentCommParameters) {
        equipmentCommParametersService.save(equipmentCommParameters, new SmartCrudPolicyAdapter() {
            @Override
            public boolean beforeInsert(CrudContext context) {
                Object o = context.getEntity();
                if (o instanceof EquipmentCommParametersDetail) {
                    EquipmentCommParameters parent = context.getParent();
                    if(parent.getChooseEnable() == 0){
                        return false;
                    }
                    ((EquipmentCommParametersDetail) o).setParaId(parent.getId());
                }
                return true;
            }
        });
        return Result.OK(equipmentCommParameters);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑公共参数", description = "编辑公共参数", method = "PUT")
    public Result<EquipmentCommParameters> edit(@RequestBody EquipmentCommParameters equipmentCommParameters) {
        equipmentCommParametersService.updateParam(equipmentCommParameters);
        return Result.OK(equipmentCommParameters);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除公共参数", description = "删除公共参数,多个id用英文逗号分隔", method = "DELETE")
    public Result<EquipmentCommParameters> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for (String key : id) {
            equipmentCommParametersService.delete(EquipmentCommParameters.class, key, new SmartCrudPolicyAdapter() {
                @Override
                public boolean beforeDelete(CrudContext context) {
                    Object o = context.getEntity();
                    if (o instanceof EquipmentCommParameters){
                        try {
                            JpaUtil.lind(EquipmentCommParametersDetail.class).equal("paraId", ((EquipmentCommParameters) o).getId()).delete();
                        } catch (Exception e) {
                            return false;
                        }
                    }
                    return true;
                }
            });
        }
        return Result.OK();
    }


}
